use async_trait::async_trait;
use base_data::channel::EndReason;
use tools::ResultNull;
use tracing::info;

use crate::{
	business::{
		bus_trait::Business,
		inbound400::context::{BusinessState, InboundRunContext},
	},
	config::business_config,
	link_to_sip::{answer, play_sound_file_to_caller},
};

#[derive(Debug)]
pub struct InvalidTarget;

#[async_trait]
impl Business for InvalidTarget {
	type C = InboundRunContext;

	async fn play_back_stop(&self, context: &mut Self::C,_channel_id:u32) -> ResultNull {
		context.business_state = BusinessState::End(EndReason::InvalidTarget);

		Ok(())
	}

	///挂断的业务逻辑
	async fn hangup(
		&self,
		context: &mut InboundRunContext,
		channel_id: u32,
		_reason: EndReason,
	) -> ResultNull {
		info!("无效目的码,收到挂断消息.id:{channel_id}");

		context.business_state = BusinessState::End(EndReason::InvalidTarget);

		Ok(())
	}

	async fn start_call(&self, context: &mut Self::C) -> ResultNull {
		info!("目的码错误。未找到相对应的目的码");
		answer(&context.caller_channel())?;

		play_sound_file_to_caller(
			context.caller_channel(),
			business_config().invalid_bell_path,
		)?;
		Ok(())
	}

	///无此业务号码，不需要上传结束通话记录
	fn need_upload_close_msg(&self) -> bool {
		false
	}
}
